﻿/**
	 * @auth Chunning
	 * @purpose some data utils
	 * @date 2009-3-19
	* */	
	

package utils.data
{
	import com.adobe.serialization.json.JSON;
	
	import flash.net.URLVariables;
		
	public class DataUtils
	{
		//START
		
		//split a array to 2 array, the split reason is the parity of the array index...
		public static function splitArrayByParity(arr:Array):Object
		{
			
			var arrOdd:Array = new Array;
			
			var arrEven:Array = new Array;
			
			for(var i:uint = 0;i < arr.length;i++)
			{
								
				if(i % 2 == 0 )
				{
					
					arrEven.push(arr[i]);	
					
				}else{
					
					arrOdd.push(arr[i]);
					
				}	
				
			}
			
			return {even:arrEven,odd:arrOdd};
			
		}
		
		//change object to xml
		public static function toXML(objInfo:Object):XML
		{
			
			var obj:Object = objInfo.hasOwnProperty("data") ? objInfo.data : null;
			
			var name:String = objInfo.name;
			
			var attributes:Object = objInfo.hasOwnProperty("attributes") ? objInfo.attributes : null;
			
			var xml:XML = new XML("<" + name + "/>");
			
			if(attributes != null)
			{
				
				for(var att:String in attributes)
				{
					
					xml["@" + att] = attributes[att];
					
				}	
				
			}
			
			if(obj == null)
			{
				
				return xml;
				
			}
						
			if(!DataUtils.hasComplexContent(obj))
			{
				
				xml.appendChild("<![CDATA[" + obj.toString() + "]]>");
				
			}else{
				
				var node:XML;
				
				var nodeName:String = objInfo.hasOwnProperty("listname") ? objInfo["listname"] : null;
				
				for(var prop:String in obj)
				{
					
					if(prop == "attributes" && attributes != null)
					{
						
						continue;
						
					}
					
					xml.appendChild(toXML({data:obj[prop],name:nodeName is String ? nodeName : prop,attributes:obj[prop].hasOwnProperty("attributes") ? obj[prop].attributes : null}));
					
				}	
				
			}			

			return xml;
			
		}
		
		//change array to XML;
		public static function convArrayToXMList(items:Array,nodeName:String = "node"):XMLList
		{
			
			var list:XMLList = new XMLList;
			
			var xmlNode:XML = new XML("<" + nodeName + "/>");
			
			for(var i:uint;i < items.length;i++)
			{
				
						
				
			}
				
			return new XMLList("<node>1</node>");	
				
		}
		
		
		//change object to urlVariable
		public static function toURLVariable(obj:Object):URLVariables
		{
			
			var uv:URLVariables = new URLVariables;
			
			for(var prop:String in obj)
			{
				
				uv[prop] = obj[prop];
				
				
			}
			
			return uv;
			
		}
		
		//change to josn
		public static function toJOSN(obj:XML):String
		{
					
			var josnCode:String = JSON.encode(obj);
			
			
			return josnCode;
			
		}
		
		//check item in a Object...
		public static function checkContain(dataSet:Object,item:Object,itemProp:String = null):String
		{
			
			var reValue:String;		
						
			for( var prop:String in dataSet)
			{
				
				try{
				
					if((itemProp == null && dataSet[prop].toString() === item.toString()) || dataSet[prop][itemProp].toString() === item.toString() )
					{
						reValue = prop;	
					
						break;
					}
					
				}catch(err:Error)
				{
					
					break;
					
				}
			} 
			
			return reValue;
			
		}
		
		//regroup the array by some property...
		public static function orderByProperty(arr:Array,prop:String = null,propOther:String = "other"):Array
		{
			
			var newArray:Array = new Array;
			
			var item:Object;
			
			var index:String;
									
			for(var i:int = 0;i < arr.length;i++)
			{
				
				index = DataUtils.checkContain(newArray,arr[i][prop],prop);
																
				if(!(index is String))
				{
					
					item = new Object;
					
					item[prop] = arr[i][prop]		
					
					item[propOther] = [arr[i]];
					
					newArray.push(item);
					
				}else{
					
					newArray[index][propOther].push(arr[i]);
															
				}
				
				delete arr[i][prop]
								
			}
			
			return newArray;
			
		}
		
		//flat the array
		public static function flatArray(arr:Array,prop1:String,prop2:String):Array
		{
			
			var newArray:Array = new Array;
			
			var others:Array;
			
			var objProp:Object;
			
			for(var i:int = 0;i< arr.length;i++)
			{
				
				objProp = arr[i][prop1];
				
				others = arr[i][prop2]
				
				if(others is Array)
				{
				
					for(var j:int = 0;j< others.length;j++)
					{										
						others[j][prop1] = objProp;
					
						newArray.push(others[j]);					
					
					}
				}
			}
			
			return newArray;
			
		}
		
		//delete exist email...
		public static function deleteExistArray(arr:Array,prop:String = null):Array
		{
			
			var newArr:Array = new Array;
			
			var index:String;
			
			for(var i:int = 0; i < arr.length;i++)
			{
				
				index = prop is String ? DataUtils.checkContain(newArr,arr[i][prop],prop) : DataUtils.checkContain(arr,arr[i]);
					
				if(index  == null)
				{
					
					newArr.push(arr[i]);	
					
				}				
								
			}
			
			return newArr;
			
		} 
		
		//check object has complex structure...
		public static function hasComplexContent(obj:Object):Boolean
		{
		
			for(var prop:String in obj)
			{
				
				return true;
				
			}	
			
			return false;
		}
		
		//check object contain...
		public static function selectArray(arrSelected:Array,condition:Object,match:Boolean = true):Array
		{
			
			var arr:Array = new Array;
			
			for(var i:uint;i < arrSelected.length;i++)
			{
									
				if(checkElement(condition,arrSelected[i]) == match)
				{
										
					arr.push(arrSelected[i]);
					
				}
								
			}
			
			return arr;	
			
		}
		
		//check contains...
		public static function checkElement(obj:Object,objElement:Object):Boolean
		{
			
			var equle:Boolean;
			
			if(hasComplexContent(obj))
			{
				
				for(var prop:String in obj)
				{
					
					if( objElement.hasOwnProperty(prop))
					{
						
						equle = checkElement(obj[prop],objElement[prop]);
						
						if(!equle)
						{
							
							break;
							
						}
						
					}else{
						
						
						equle = false;
						
						break;
						
					}				
					
				}
				
			}else{
								
				if(obj is RegExp)
				{
					
					(obj as RegExp).lastIndex = 0;
					
					equle = (obj as RegExp).test(objElement.toString());
					
				}else{
															
					equle = obj.toString() == objElement.toString();	
				
				}
			
			}
			
						
			return equle;
			
		}
		
		//select a property form a item of array, then group them to a new array...
		public static function createArryByProperty(arr:Array,prop:String):Array
		{
			
			var newArray:Array = new Array;
			
			for(var i:int = 0;i < arr.length;i++)
			{
				
				if(arr[i].hasOwnProperty(prop))
				{
					
					newArray.push(arr[i][prop]);
					
				}
				
			} 
			
			return newArray;
			
		}
		
		//trim...
		public static function trim(val:Object):String
		{
	
			var str:String = String(val);
			
			str = str.replace(/^\s*/gi,"");
			
			str = str.replace(/\s*$/gi,"");
			
			return str;
		
		}
		
		//convert propety to appropriate data type...
		public static function convertDataType(data:Object):Object
		{
			
			var objConv:Object = new Object;
			
			for(var prop:String in data)
			{
												
				if(hasComplexContent(data[prop]))
				{
					
					
					objConv[prop] = convertDataType(data[prop]);
					
					
				}else{					
					
					objConv[prop] = Number(data[prop]);
					
					objConv[prop] = isNaN(objConv[prop]) ? data[prop] : objConv[prop];
						
					objConv[prop] = String(objConv[prop]).toLowerCase() == "true" ? true : 	objConv[prop];
					
					objConv[prop] = String(objConv[prop]).toLowerCase() == "false" ? false : 	objConv[prop];
					
				}
								
			}			
			
			return objConv;
			
			
		}
		
		
	//END	
	}
}